In this step of the tutorial you first define the data source for a static list in the data source plugin and then use it in the Kanzi Studio project to get the data for items in a Grid List Box node from a data source.
In this section you add the functionality which enables the data source plugin to generate list data objects.
To add the functionality to generate list data objects:
// Provides the functionality for creating data objects for a static list. #include "data_object_static_list.hpp"
onTimer
function create a function which converts the content of the XML structure in the memory to list items in the static list data object.static void addDataObjectsRecursively(Domain* domain, DataObjectSharedPtr object, const tinyxml2::XMLElement* xml); // Use this function to add list items to a list data object. static void addListItems(Domain* domain, DataObjectStaticListSharedPtr list, const tinyxml2::XMLElement* itemsElement) { for (const tinyxml2::XMLElement* child = itemsElement->FirstChildElement(); child; child = child->NextSiblingElement()) { // Create the root data object for the list item hierarchy. DataObjectSharedPtr rootObject = make_shared<DataObject>(domain, "Item root"); // Create the list item tree recursively. addDataObjectsRecursively(domain, rootObject, child); // Add the first child from the root data object as a list item. Discard the root object. list->addItem(rootObject); } }
addDataObject
function a condition to create a list type data object from the list type attribute.// Add a data object of the type specified by the type attribute in the XML element. Get the initial value from the text parameter. DataObjectSharedPtr addDataObject(Domain *domain, const char* type, const char* name, const char* text) { ... // Create a list data object from the list type attributes. else if (type && strcmp(type, "list") == 0) { object = make_shared<DataObjectStaticList>(domain, name); } ... }
addDataObjectsRecursively
function to add the functionality to create data objects inside a list data object, if the name of the XML element within the list type is called items.static void addDataObjectsRecursively(Domain* domain, DataObjectSharedPtr parent, const tinyxml2::XMLElement* xml) { ... // Traverse the tree in the XML file to add data objects for each child element of the current XML element. for (const tinyxml2::XMLElement* child = xml->FirstChildElement(); child; child = child->NextSiblingElement()) { // Recurse. addDataObjectsRecursively(domain, object, child); } }
static void addDataObjectsRecursively(Domain* domain, DataObjectSharedPtr parent, const tinyxml2::XMLElement* xml) { ... // Check whether the type of the current data object is list. DataObjectStaticListSharedPtr list = dynamic_pointer_cast<DataObjectStaticList>(object); // Traverse the tree in the XML file to add data objects for each child element of the current XML element. for (const tinyxml2::XMLElement* child = xml->FirstChildElement(); child; child = child->NextSiblingElement()) { // If the name of the XML element within the list type is items, create data objects inside that list data object. if (list && strcmp(child->Name(), "items") == 0) { addListItems(domain, list, child); } else { // Recurse. addDataObjectsRecursively(domain, object, child); } } }
In this section you set a Grid List Box 2D node in the project to get data for its items from a list data object, which the plugin generates from the data source.
To get list data from a data source:
The list of contacts now receives the data from the ClusterWithList.xml data source. Use a text editor to change the values in the ClusterWithList.xml file, save the ClusterWithList.xml file, and Kanzi Studio Preview and the Data Sources window show the updated data objects and their values.
In this tutorial you learned how to define an XML data source and how to use the data from that data source in a Kanzi Studio project to provide the data to a Kanzi application. Now you can:
To learn more about the data sources in Kanzi, see Data sources.